<?php

/**
 * 文章管理
 * @author  Mr.L <349865361@qq.com>
 */

namespace app\shop\admin;

class ContentAdmin extends \app\system\admin\SystemExtendAdmin {

    protected $_model = 'Shop';

    /**
     * 模块信息
     */
    protected function _infoModule() {
        return [
            'info' => [
                'name' => '商品管理',
                'description' => '管理站点中的商品信息',
            ],
            'fun' => [
                'index' => true,
                'add' => true,
                'edit' => true,
                'del' => true,
                'status' => true
            ]
        ];
    }

    public function _indexParam() {
        return [
            'class_id' => 'B.class_id',
            'pos_id' => 'pos_id',
            'keyword' => 'A.title'
        ];
    }

    protected function _indexWhere($whereMaps) {

        if($whereMaps['pos_id']) {
            $whereMaps['_sql'] = 'FIND_IN_SET('.$whereMaps['pos_id'].', A.pos_id)';
        }

        unset($whereMaps['pos_id']);
        return $whereMaps;
    }

    public function _indexOrder() {
        return 'A.sort asc, A.create_time desc, B.shop_id desc';
    }


    public function _indexAssign($pageMaps) {
        $classId = $pageMaps['class_id'];
        return array(
            'posList' => target('site/SitePosition')->loadList(),
            'treeHtml' => target('shop/ShopClass')->getHtmlTree(0, $classId, ['parent_id', 'class_id', 'name']),
            'classId' => $classId,
        );
    }

    public function _addAssign() {
        $classId = request('get', 'class_id', 0, 'intval');
        $classInfo = target('shop/ShopClass')->getInfo($classId);

        //扩展模型
        $modelId = intval($classInfo['model_id']);
        $modelHtml = target('site/SiteModel')->getHtml($modelId);

        //预设属性
        $filterId = intval($classInfo['filter_id']);
        $filterHtml = target('site/SiteFilter')->getHtml($filterId);
        $filterList = target('site/SiteFilter')->loadList();


        $hookList = run('service', 'shop', 'adminContentHtml');
        $hookHtml = '';
        foreach ($hookList as $app => $vo) {
            if (!empty($vo)) {
                $hookHtml .= $vo;
            }
        }
        return array(
            'tplList' => target('site/SiteTpl')->tplFiles(),
            'classList' => target('shop/ShopClass')->loadTreeList(['A.model_id' => $modelId]),
            'posList' => target('site/SitePosition')->loadList(),
            'brandList' => target('shop/ShopBrand')->loadList(),
            'modelHtml' => $modelHtml,
            'filterHtml' => $filterHtml,
            'hookHtml' => $hookHtml,
            'classId' => $classId,
            'proDataJson' => json_encode([]),
            'proHeadJson' => json_encode([]),
            'productNo' => \app\shop\unit\SequenceNumber::get(time(), 12, 'D'),
            'filterList' => $filterList
        );
    }

    public function _editAssign($info) {
        $classId = intval($info['class_id']);
        $modelId = intval($info['model_id']);
        $classInfo = target('shop/ShopClass')->getInfo($classId);

        //扩展模型
        $modelInfo = target('site/SiteModel')->getContent($modelId, $info['content_id']);
        $modelHtml = target('site/SiteModel')->getHtml($modelId, $modelInfo);

        //内容属性
        $filterId = intval($info['filter_id']);
        $filterHtml = target('site/SiteFilter')->getHtml($filterId, $info['content_id']);
        $filterList = target('site/SiteFilter')->loadList();

        //重组产品数据
        $proData = array();
        $proHead = array();
        $headStatus = false;
        $proList = target('shop/ShopProducts')->loadList(array('shop_id' => $info['shop_id']), 0);

        foreach ($proList as $key => $vo) {
            $specData = unserialize($vo['spec_data']);
            if (!$headStatus) {
                if (!empty($specData)) {
                    foreach ($specData as $k => $v) {
                        $proHead[$k] = array(
                            'id' => $v['id'],
                            'name' => $v['name'],
                        );
                    }
                }
                $headStatus = true;
            }
            $proData['spec_list'][$key] = $specData;
            $proData['id'][$key] = $vo['products_id'];
            $proData['goods_no'][$key] = $vo['products_no'];
            $proData['sell_price'][$key] = $vo['sell_price'];
            $proData['market_price'][$key] = $vo['market_price'];
            $proData['cost_price'][$key] = $vo['cost_price'];
            $proData['store'][$key] = $vo['store'];
            $proData['weight'][$key] = $vo['weight'];
            $proData['point'][$key] = $vo['point'];
        }

        return array(
            'tplList' => target('site/SiteTpl')->tplFiles(),
            'classList' => target('shop/ShopClass')->loadTreeList(['A.model_id' => $modelId]),
            'filterAttr' => target('site/SiteFilterAttr')->loadList(['filter_id' => $classInfo['filter_id']]),
            'posList' => target('site/SitePosition')->loadList(),
            'brandList' => target('shop/ShopBrand')->loadList(),
            'modelHtml' => $modelHtml,
            'filterHtml' => $filterHtml,
            'classId' => $classId,
            'proDataJson' => json_encode($proData),
            'proHeadJson' => json_encode($proHead),
            'productNo' => \app\shop\unit\SequenceNumber::get(time(), 12, 'D'),
            'filterList' => $filterList
        );
    }


    protected function _indexUrl($id) {
        return url('index', array('class_id' => request('post', 'class_id')));
    }

    public function _statusData($id, $status) {
        $info = target('site/SiteContent')->getInfo($id);
        return target('site/SiteContent')->where([
            'content_id' => $info['content_id']
        ])->data([
            'status' => $status
        ])->update();
    }

    public function specDialog() {

        $classId = request('get', 'class_id');
        $classInfo = target('shop/ShopClass')->getInfo($classId);
        $groupInfo = target('shop/ShopSpecGroup')->getInfo($classInfo['spec_group_id']);
        $specIds = $groupInfo['spec_ids'] ? explode(',' , $groupInfo['spec_ids']) : [];

        $specList = target('shop/ShopSpec')->loadList();
        $specGroup = target('shop/ShopSpecGroup')->loadList();

        $specGroupNull = [];
        $specData = [];

        foreach ($specList as $key => $vo) {
            $specList[$key]['value'] = explode(',', $vo['value']);
            $specData[$vo['spec_id']] = $specList[$key];
            if ($specGroup) {
                foreach ($specGroup as $k => $v) {
                    $specId = explode(',', $v['spec_ids']);
                    if (in_array($vo['spec_id'], $specId)) {
                        $specGroup[$k]['spec'][] = $vo;
                    } else {
                        $specGroupNull[] = $vo;
                    }
                }
            } else {
                $specGroupNull[] = $vo;
            }
        }

        $specGroup[] = [
            'group_id' => 0,
            'name' => '未分组',
            'spec' => $specGroupNull
        ];


        $this->assign('specIds', $specIds);
        $this->assign('specList', $specData);
        $this->assign('specGroup', $specGroup);
        $this->dialogDisplay();
    }


}